import 'package:enterprise_v6_app/app/components/custom_modele/custom_gradient_line.dart';
import 'package:enterprise_v6_app/app/components/custom_modele/custom_progress_comp_page.dart';
import 'package:enterprise_v6_app/app/components/custom_modele/custom_table.dart';
import 'package:enterprise_v6_app/app/components/special_operation/custom_title_chart_page.dart';
import 'package:enterprise_v6_app/app/core/core.dart';
import 'package:enterprise_v6_app/app/data/common/charts_pie.dart';
import 'package:enterprise_v6_app/app/modules/application/double_prevention_page/danger_investigation_page/home/home_controller.dart';
import 'package:syncfusion_flutter_charts/charts.dart';

class DangerInvestigationHomePage extends StatelessWidget {
  DangerInvestigationHomePage({super.key});

  final controller = Get.put(DangerInvestigationHomeController());

  @override
  Widget build(BuildContext context) {
    return GetBuilder(
      init: controller,
      builder: (_) {
        return Scaffold(
          backgroundColor: AppColors.bg_color_grey,
          body: ListView(
            padding: EdgeInsets.symmetric(vertical: 16.w, horizontal: 14.w),
            children: [
              _buildCheckMission(),
              AppGaps.vGap10,
              _buildCheckPlan(),
              AppGaps.vGap10,
              _buildAssessment(),
              AppGaps.vGap10,
              _buildAlarm(),
            ],
          ),
        );
      },
    );
  }

  /// 排查任务
  Widget _buildCheckMission() {
    return CustomTitleChartPage(
      title: '排查任务',
      child: _buildCheckMissionChart(),
    );
  }

  /// 排查计划
  Widget _buildCheckPlan() {
    return CustomTitleChartPage(
      title: '排查计划',
      child: _buildCheckPlanChart(),
    );
  }

  /// 圈图 右侧解释
  Widget _buildLegendItem(Color? color, String name, int count, {double? height}) {
    return Container(
      width: 100.w,
      height: height ?? 23.w,
      alignment: Alignment.center,
      margin: EdgeInsets.only(top: 7.w),
      child: Row(
        children: [
          Container(
            width: 10.w,
            height: 10.w,
            margin: EdgeInsets.only(right: 5.w),
            padding: EdgeInsets.all(2.w),
            decoration: BoxDecoration(color: color, borderRadius: BorderRadius.circular(5.w)),
            child: Container(
              decoration: BoxDecoration(color: Colors.white, borderRadius: BorderRadius.circular(5.w)),
            ),
          ),
          Text(
            name,
            style: TextStyle(fontSize: 12.w, fontWeight: FontWeight.w400, color: const Color(0xffA0A7B5)),
          ),
          const Spacer(),
          Text(
            '$count',
            style: TextStyle(
              fontSize: 12.sp,
              fontWeight: FontWeight.w400,
              color: Colors.black,
            ),
          ),
        ],
      ),
    );
  }

  /// 排查任务 图表
  Widget _buildCheckMissionChart() {
    final annoCenter = Container(
      width: 56.w,
      height: 56.w,
      decoration: BoxDecoration(
        color: Colors.white,
        borderRadius: BorderRadius.circular(50.w),
      ),
      child: Column(
        mainAxisAlignment: MainAxisAlignment.center,
        children: [
          Text(
            '排查任务',
            style: TextStyle(
              fontSize: 12.w,
              fontWeight: FontWeight.w400,
              color: const Color(0xffA0A7B5),
            ),
          ),
          Text(
            '${controller.missionDataCount}',
            style: TextStyle(fontSize: 16.w, fontWeight: FontWeight.w400, color: const Color(0xFF495061)),
          ),
        ],
      ),
    );
    return SizedBox(
      height: 105.w,
      child: SfCircularChart(
        legend: Legend(
          isVisible: true,
          height: 123.w.toString(),
          position: LegendPosition.right,
          overflowMode: LegendItemOverflowMode.none,
          legendItemBuilder: (String name, dynamic series, dynamic point, int seriesIndex) {
            final item = controller.missionData[seriesIndex];
            return _buildLegendItem(
              item.color,
              name,
              item.y.toInt(),
            );
          },
          toggleSeriesVisibility: false,
        ),
        annotations: [
          CircularChartAnnotation(
            widget: controller.missionDataCount == 0
                ? Stack(
                    alignment: Alignment.center,
                    children: [
                      annoCenter,
                      SizedBox(
                        width: 75.w,
                        height: 75.w,
                        child: CircularProgressIndicator(
                          strokeWidth: 10.w,
                          value: 1,
                          valueColor: const AlwaysStoppedAnimation<Color>(Color(0xFF33A9FF)),
                        ),
                      ),
                    ],
                  )
                : annoCenter,
          ),
        ],
        series: <CircularSeries>[
          DoughnutSeries<ChartData, String>(
            dataSource: controller.missionData,
            pointColorMapper: (ChartData data, _) => data.color,
            xValueMapper: (ChartData data, _) => data.x,
            yValueMapper: (ChartData data, _) => data.y,
            name: '排查任务',
            dataLabelSettings: const DataLabelSettings(
              isVisible: false,
              labelPosition: ChartDataLabelPosition.inside,
            ),
            radius: '100%',
            innerRadius: '75%',
            enableTooltip: true,
          ),
        ],
      ),
    );
  }

  /// 排查计划 图表
  Widget _buildCheckPlanChart() {
    final annoCenter = Container(
      width: 56.w,
      height: 56.w,
      decoration: BoxDecoration(
        color: Colors.white,
        borderRadius: BorderRadius.circular(50.w),
      ),
      child: Column(
        mainAxisAlignment: MainAxisAlignment.center,
        children: [
          Text(
            '排查计划',
            style: TextStyle(
              fontSize: 12.w,
              fontWeight: FontWeight.w400,
              color: const Color(0xffA0A7B5),
            ),
          ),
          Text(
            '${controller.planDataCount}',
            style: TextStyle(fontSize: 16.w, fontWeight: FontWeight.w400, color: const Color(0xFF495061)),
          ),
        ],
      ),
    );
    return SizedBox(
      height: 105.w,
      child: SfCircularChart(
        legend: Legend(
          isVisible: true,
          height: 123.w.toString(),
          position: LegendPosition.right,
          overflowMode: LegendItemOverflowMode.none,
          legendItemBuilder: (String name, dynamic series, dynamic point, int seriesIndex) {
            final item = controller.planData[seriesIndex];
            return _buildLegendItem(
              item.color,
              name,
              item.y.toInt(),
            );
          },
          toggleSeriesVisibility: false,
        ),
        annotations: [
          CircularChartAnnotation(
            widget: controller.missionDataCount == 0
                ? Stack(
                    alignment: Alignment.center,
                    children: [
                      annoCenter,
                      SizedBox(
                        width: 75.w,
                        height: 75.w,
                        child: CircularProgressIndicator(
                          strokeWidth: 10.w,
                          value: 1,
                          valueColor: const AlwaysStoppedAnimation<Color>(Color(0xFF33A9FF)),
                        ),
                      ),
                    ],
                  )
                : annoCenter,
          ),
        ],
        series: <CircularSeries>[
          DoughnutSeries<ChartData, String>(
            dataSource: controller.planData,
            pointColorMapper: (ChartData data, _) => data.color,
            xValueMapper: (ChartData data, _) => data.x,
            yValueMapper: (ChartData data, _) => data.y,
            name: '排查计划',
            dataLabelSettings: const DataLabelSettings(
              isVisible: false,
              labelPosition: ChartDataLabelPosition.inside,
            ),
            radius: '100%',
            innerRadius: '75%',
            enableTooltip: true,
          ),
        ],
      ),
    );
  }

  /// 机制运行效果及评估
  Widget _buildAssessment() {
    return CustomTitleChartPage(
      title: '机制运行效果及评估',
      titleRightWidget: Stack(
        alignment: Alignment.center,
        children: [
          SizedBox(
            width: 28.w,
            height: 18.w,
            child: SvgPicture.asset(AppImages.icon_red_trapezoid),
          ),
          Text(
            '差',
            style: TextStyle(color: Colors.white, fontSize: 10.w),
          )
        ],
      ),
      child: Column(
        children: [
          CustomProgressComp(
            width: 263.w,
            height: 6.w,
            bgColor: const Color(0xFFEFF7FF),
            topTitle: _buildProgressTitle('风险分析完成率'),
            linearGradient: const LinearGradient(
              begin: Alignment.centerRight,
              end: Alignment.centerLeft,
              colors: [Color(0xFF9DCFFF), Color(0xFF33A9FF)],
            ),
            value: 0.8,
            title: '12',
            isShowTitle: false,
            isShowIcon: true,
          ),
          CustomProgressComp(
            width: 263.w,
            height: 6.w,
            bgColor: const Color(0xFFEFF7FF),
            topTitle: _buildProgressTitle('排查任务完成率'),
            linearGradient: const LinearGradient(
              begin: Alignment.centerRight,
              end: Alignment.centerLeft,
              colors: [Color(0xFF9DCFFF), Color(0xFF33A9FF)],
            ),
            value: 0.5,
            title: '12',
            isShowTitle: false,
            isShowIcon: true,
          ),
          CustomProgressComp(
            width: 263.w,
            height: 6.w,
            bgColor: const Color(0xFFEFF7FF),
            topTitle: _buildProgressTitle('隐患整改完成率'),
            linearGradient: const LinearGradient(
              begin: Alignment.centerRight,
              end: Alignment.centerLeft,
              colors: [Color(0xFF9DCFFF), Color(0xFF33A9FF)],
            ),
            value: 0.5,
            title: '12',
            isShowTitle: false,
            isShowIcon: true,
          ),
        ],
      ),
    );
  }

  /// 机制运行效果及评估 进度条 标题
  Widget _buildProgressTitle(String title) {
    return Row(
      children: [
        SizedBox(
          width: 100.w,
          child: Text(
            title,
            style: TextStyle(
              fontSize: 12.sp,
              fontWeight: FontWeight.w400,
              color: const Color(0xff333333).withOpacity(0.5),
            ),
          ),
        ),
        Icon(Icons.arrow_left_outlined, size: 20.sp, color: const Color(0xffFD6B6B)),
        Text(
          '总数：50',
          style: TextStyle(
            fontSize: 12.sp,
            fontWeight: FontWeight.w400,
            color: const Color(0xff495061),
          ),
        ),
        const Spacer(),
        Text(
          '80%',
          style: TextStyle(
            fontSize: 12.sp,
            fontWeight: FontWeight.w500,
            color: const Color(0xff417EFB),
          ),
        ),
      ],
    );
  }

  /// 消警
  Widget _buildAlarm() {
    return CustomTitleChartPage(
      title: '消警',
      child: Column(
        children: [
          _buildAlarmSummary(),
          AppGaps.vGap10,
          _buildHistory(),
        ],
      ),
    );
  }

  /// 消警 总览
  Widget _buildAlarmSummary() {
    return Container(
      decoration: BoxDecoration(color: const Color(0x1A5C6DFF), borderRadius: BorderRadius.all(Radius.circular(8.w))),
      child: Padding(
        padding: EdgeInsets.all(10.w),
        child: Row(
          mainAxisAlignment: MainAxisAlignment.spaceAround,
          children: [
            _buildCancelAlarmSummaryTop(0, '未处理'),
            CustomVerticalGradientLine(height: 24.w),
            _buildCancelAlarmSummaryTop(0, '消警'),
            CustomVerticalGradientLine(height: 24.w),
            _buildCancelAlarmSummaryTop(0, '超时'),
            CustomVerticalGradientLine(height: 24.w),
            _buildCancelAlarmSummaryTop(0, '误报'),
          ],
        ),
      ),
    );
  }

  /// 消警 总览 顶部 单个
  Widget _buildCancelAlarmSummaryTop(int n, String title) {
    const color = Color.fromARGB(255, 102, 116, 245);
    return Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        Text(
          n.toString(),
          style: const TextStyle(
            color: color,
            fontWeight: FontWeight.w500,
          ),
        ),
        AppGaps.vGap10,
        Text(
          title,
          style: const TextStyle(
            color: color,
            fontWeight: FontWeight.w500,
          ),
        ),
      ],
    );
  }

  /// 消警 历史
  Widget _buildHistory() {
    return CustomTable(
      xLen: 4,
      yLen: 3,
      alignment: Alignment.center,
      cellPadding: EdgeInsets.all(6.w),
      cellBuilder: (x, y) {
        return Text(
          '$x * $y',
          style: TextStyle(
            color: const Color(0xFF818499),
            fontSize: 12.w,
          ),
        );
      },
      headBuilder: (x) => Text(
        controller.tableTitle[x],
        style: TextStyle(
          fontWeight: FontWeight.w500,
          fontSize: 12.w,
        ),
      ),
    );
  }
}
